package de.clapp.backend.requests;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Type;

public class BitmapRequest implements ServerRequest
{
    /**
     * 
     */
    private static final long serialVersionUID = -1360219111762638379L;

    private String pathToBitmap;

    private byte[] data;

    public BitmapRequest(String pathToBitmap)
    {
        this.pathToBitmap = pathToBitmap;
    }

    @Override
    public byte[] getItem()
    {
        return this.data;
    }

    @Override
    public void setItem(Object item)
    {
        this.data = (byte[]) item;
    }

    @Override
    public Type getReturnType()
    {
        return byte[].class;
    }

    @Override
    public Object execute()
    {
        File file = new File(this.pathToBitmap);
        byte[] data = new byte[(int) file.length()];
        try
        {
            FileInputStream fis = new FileInputStream(file);
            fis.read(data);
            fis.close();
        }
        catch (FileNotFoundException e)
        {
            return new byte[1];
        }
        catch (IOException e)
        {
            return new byte[1];
        }

        return data;
    }

}
